<template>
	<div class="left-box">
		<transfer-tree
			:checkable="true"
			:defaultExpandedKeys="defaultExpandedKeys"
			:checkedList="checkedList"
			:treeData="treeData"
			:isUseDefaultIcon="false"
			:isCustomIcon="true"
			@toCheckedNode="onChecked"
		/>
	</div>
</template>

<script>
import transferTree from '@/components/transferTree.vue';
import { mapActions, mapState } from 'vuex';
import { ACTION_TOGGLE_ENTITY } from '@/store/actionTypes';
import { getPanelData } from '@/api/modules/SelectOrgApi';
import { EntityTypes } from '@/utils/orgConstants';

export default {
	name: 'OrgMetadataTag',
	components: {
		transferTree
	},
	inject: ['app'],
	data() {
		return {
			treeData: [],
			checkedList: [],
			defaultExpandedKeys: [],
			orgMetadataTagTypes: 'Account,Department,Post,Member'

		};
	},
	computed: {
		...mapState('selectData', ['selectedEntityMap']),
		orgSelect() {
			return this.app().orgSelect;
		}
	},
	watch: {
		selectedEntityMap: {
			deep: true,
			handler(selectedEntity) {
				this.onTreeNodeCheckedList(selectedEntity);
			}
		}
	},
	methods: {
		...mapActions('selectData', [ACTION_TOGGLE_ENTITY]),
		async onGetPanelData(data = {}) {
			this.$changeAccount(this, data.accountId || data.orgMetadataTagTypes);
			let params = {
				command: 'orgMetadataTree',
				panel: 'OrgMetadataTag',
				arguments: data
			};
			let res = await getPanelData(params);
			if (Number(res.code) !== 0) {
				return;
			}
			let treeData = res.data;
			this.dealNodeCheckbox(treeData);
			if (treeData.length) {
				this.$set(this.defaultExpandedKeys, 0, treeData[0].id);
			}
			this.treeData = treeData;
		},
		/**
		 * 处理部分节点不能用Checkbox
		 * @param nodes 节点
		 */
		dealNodeCheckbox(nodes) {
			for (let node of nodes) {
				node.nocheck = !(node.type.indexOf(EntityTypes.TP_OrgMetadataTag) === -1);
				if (node.children && node.children.length) {
					this.dealNodeCheckbox(node.children);
				}
			}
		},
		onChecked(data, node) {
			if (!data.checkeds.length) {
				return;
			}
			let type = data.checkeds[0].type;
			if (type.indexOf(EntityTypes.TP_OrgMetadataTag) === -1) {
				// TP_开头的数据不能被选择
				this[ACTION_TOGGLE_ENTITY]({
					entities: data.checkeds
				});
			}
		},
		onChangeAccount(data) {
			let params = {accountId: data.id};
			if (this.orgMetadataTagTypes !== undefined && this.orgMetadataTagTypes !== '') {
				params.orgMetadataTagTypes = this.orgMetadataTagTypes;
			}
			this.onGetPanelData(params);
		},
		onTreeNodeCheckedList(selectedEntity) {
			let checkedList = [];
			let typeIds = Object.keys(selectedEntity);
			for (let item of typeIds) {
				if (item.indexOf(EntityTypes.OrgMetadataTag) !== -1) {
					checkedList.push(item.split('|')[1]);
				}
			}
			this.checkedList = checkedList;
		}
	},
	created() {
		let accountId = this.app().switchAfterUnit.id;
		this.orgMetadataTagTypes = this.orgSelect.orgMetadataTagTypes;
		let params = {accountId};
		if (this.orgMetadataTagTypes !== undefined && this.orgMetadataTagTypes !== '') {
			params.orgMetadataTagTypes = this.orgMetadataTagTypes;
		}
		this.onTreeNodeCheckedList(this.selectedEntityMap);
		this.onGetPanelData(params);
	},
	activated() {
		const { id: accountId } = this.app().switchAfterUnit;
		console.log('activated===>accountId', accountId);
		if (this.$isChangeAccount(this, accountId)) {
			this.onGetPanelData({accountId});
		}
	}
};
</script>
